package com.epoch.servicetest.service.impl;

import bean.response.ResponseBean;
import bean.response.ResultEnum;
import com.epoch.servicetest.dao.StuTestDao;
import com.epoch.servicetest.dao.TestDao;
import com.epoch.servicetest.service.StuTestService;
import com.epoch.servicetest.test.StuTest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import utils.AssertUtil;
import utils.ResultInfoUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author mi
 */
@Service
@Slf4j
public class StuTestServiceImpl implements StuTestService {
    @Resource
    private StuTestDao stuTestDao;

    @Resource
    private TestDao testDao;

    /**
     * select test
     * @param csId
     * @return StuTest
     */
    @Override
    public List<Object> getStuTestById(String csId){
        AssertUtil.isTrue(StringUtils.isBlank(csId),"用户编号不能为空！");
        List<StuTest> stuTestList = stuTestDao.getStuTestById(csId);
        List<Object> resultList = new ArrayList<>();
        for(StuTest list:stuTestList){
            HashMap<String,Object> stuTestMap = new HashMap(8);
            stuTestMap.put("times",list.getTimes());
            stuTestMap.put("testName",testDao.getTestById(list.getTestId()).getTestName());
            stuTestMap.put("correct",(list.getCorrect()+'%'));
            resultList.add(stuTestMap);
        }
        return resultList;
    }

    @Override
    public ResponseBean getStuTestByCsId(String csId) {
        List<StuTest> stuTestList = stuTestDao.getStuTestById(csId);
        System.out.println(stuTestList.size());
        List<HashMap<String, Object>> result = new ArrayList<>(stuTestList.size());
        for (int i = 0; i < stuTestList.size(); i++) {
            HashMap<String, Object> stuTestMap = new HashMap<>();
            stuTestMap.put("times",stuTestList.get(i).getTimes());
            stuTestMap.put("correctRate",stuTestList.get(i).getCorrect());
            stuTestMap.put("testName",testDao.getTestById(stuTestList.get(i).getTestId()).getTestName());
            result.add(stuTestMap);
        }
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"获取学生做题记录成功",result);
    }

}
